package com.eco.fanliapp.service.impl;

import com.eco.fanliapp.entity.AppCompany;
import com.eco.fanliapp.entity.AppGeneralFeeScheme;
import com.eco.fanliapp.entity.AppOrderFeeScheme;
import com.eco.fanliapp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 用户佣金比例计算逻辑和查询用户所属公司
 * @author xiaoxx
 * @Time 2019年5月29日下午3:50:30
 */
@Service
public class UserCashschemServiceImpl implements IUserCashschemeService {


	@Autowired
	private IAppUserOrderService iAppUserOrderService;
	@Autowired
	private IAppCompanyService iAppCompanyService;
	@Autowired
	private IAppGeneralfeeschemeService appGeneralfeeschemeService;
	@Autowired
	private RedisTemplateService redisTemplateService;

	//用户佣金比例计算逻辑
	@Override
	public Integer getCommissionRate(Long userId,double commission) {
		AppCompany appCompany = findCompany(userId);
		//杀熟分佣
		AppOrderFeeScheme orderFeeScheme = appCompany.getOrderFeeScheme();
		if(orderFeeScheme!=null){
		int countOrderNum = iAppUserOrderService.countBingOrderSum(userId);
			if (orderFeeScheme.getOneorderScale() > countOrderNum) {
				return  orderFeeScheme.getOneorderScale();
			} else if (orderFeeScheme.getTwoorder() > countOrderNum) {
				return  orderFeeScheme.getTwoorderScale();
			} else if (orderFeeScheme.getThirdorder() > countOrderNum) {
				return  orderFeeScheme.getThirdorderScale();
			}
		}
		
		//普通分佣
		AppGeneralFeeScheme generalFeeScheme = appCompany.getGeneralFeeScheme();
		if(generalFeeScheme!=null){
			if (generalFeeScheme.getOnepay() >= commission) {
				return generalFeeScheme.getOnepayScale();
			} else if (generalFeeScheme.getTwopay() >= commission) {
				return generalFeeScheme.getTwopayScale();
			} else if (generalFeeScheme.getThirdpay() >= commission) {
				return generalFeeScheme.getThirdpayScale();
			} else if (generalFeeScheme.getForpay() >= commission) {
				return generalFeeScheme.getForpayScale();
			} else if (generalFeeScheme.getFivepay() >= commission) {
				return generalFeeScheme.getFivepayScale();
			}else if (generalFeeScheme.getSixpay() >= commission) {
				return generalFeeScheme.getSixpayScale();
			}else if (generalFeeScheme.getSevenpay() >= commission) {
				return generalFeeScheme.getSevenpayScale();
			}else {
				return generalFeeScheme.getSevenpayScale();
			}
		}
		return 0;
	}

	//订单的用户佣金比例计算逻辑
	public Object getSheme(Long userId,double commission) {
		AppCompany appCompany = findCompany(userId);
		//杀熟分佣
		AppOrderFeeScheme orderFeeScheme = appCompany.getOrderFeeScheme();
		if(orderFeeScheme!=null){
			int countOrderNum = iAppUserOrderService.countBingOrderSum(userId);
			if (orderFeeScheme.getOneorderScale() > countOrderNum) {
				return  orderFeeScheme;
			} else if (orderFeeScheme.getTwoorder() > countOrderNum) {
				return  orderFeeScheme;
			} else if (orderFeeScheme.getThirdorder() > countOrderNum) {
				return  orderFeeScheme;
			}
		}
		//普通分佣
		AppGeneralFeeScheme generalFeeScheme = appCompany.getGeneralFeeScheme();
		return  generalFeeScheme;
	}

	//订单的用户佣金比例计算逻辑--部分用户高佣，走特殊渠道
	public Object getShemeNew(Long userId,double commission) {


		Set<String> userIds = new HashSet<>();
		userIds.add("601987531125");
		userIds.add("601761942295");
		userIds.add("600520970757");
		userIds.add("600665059291");
//		userIds.add("");
//		userIds.add("");

		if (userIds.contains(userId + "")) {
			AppGeneralFeeScheme appGeneralFeeScheme = appGeneralfeeschemeService.selectAppGeneralfeeschemeById(24, 0);
			if (appGeneralFeeScheme != null) {
				return  appGeneralFeeScheme;
			}
		}
		AppCompany appCompany = findCompany(userId);
		//杀熟分佣
		AppOrderFeeScheme orderFeeScheme = appCompany.getOrderFeeScheme();
		if(orderFeeScheme!=null){
			int countOrderNum = iAppUserOrderService.countBingOrderSum(userId);
			if (orderFeeScheme.getOneorderScale() > countOrderNum) {
				return  orderFeeScheme;
			} else if (orderFeeScheme.getTwoorder() > countOrderNum) {
				return  orderFeeScheme;
			} else if (orderFeeScheme.getThirdorder() > countOrderNum) {
				return  orderFeeScheme;
			}
		}
		//普通分佣
		AppGeneralFeeScheme generalFeeScheme = appCompany.getGeneralFeeScheme();
		return  generalFeeScheme;

	}
	//查询用户所属公司
	@Override
	public AppCompany findCompany(Long userId) {
		// TODO sql逻辑实现 根据用户获取所属公司
		AppCompany appCompany = iAppCompanyService.findtAppCompany(userId);
		return appCompany;
	}

	//查询总公司
	@Override
	public AppCompany findCompanyAll() {
		// TODO Auto-generated method findCompanyAll
		String key = "app_company_find_all";
		AppCompany appCompany = null;
		if(redisTemplateService.hasKey(key)){
			appCompany = redisTemplateService.getTByKey(key,AppCompany.class);
		}
		if(appCompany == null){
			appCompany = iAppCompanyService.findCompanyAll();
			redisTemplateService.set(key,appCompany,60*5);
		}
		return appCompany;
	}


}
